package com.sh.bs.common.base

import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.asynclayoutinflater.view.AsyncLayoutInflater

/**
 * 基类Activity
 */
abstract class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (useAsyncLayout()) {
            AsyncLayoutInflater(this).inflate(
                getLayoutId(), null
            ) { view, resId, parent ->
                setContentView(view)
                initViews(view)
            }
        } else {
            setContentView(getLayoutId())
            initViews()
        }
    }

    /**
     * 布局文件
     */
    @LayoutRes
    protected abstract fun getLayoutId(): Int

    /**
     * 初始化控件
     */
    protected abstract fun initViews(view: View? = null)

    /**
     * 是否启用异步加载布局
     */
    protected fun useAsyncLayout(): Boolean = true
}